// pages/userInfoCenter/index.js
const App = getApp();
const {Dialog, Toast} = require("../../../../../../config/modal")
const utils = require("../../../../../../utils/util")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        copyImg: "/static/img/copy.png",
        title: "积分兑换记录",
        // 展开物流
        showMore: {
            showText: "查看物流",
            show: false,
        },
        offsetTop: App.globalData.window.navHeight,
        theamTextColor: App.globalData.theamTextColor,
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        records: {
            list: [],
            activeKey: 0,
            goodsType: [],
            progress: {},
            progressActive: 0,
            address: {},
            currentRecord: {}
        },
        searchParams: "",
        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0,
        },
    },
    URL: {
        getList: "/user/integral/goods/getList",
        progress: "/user/integral/goods/getProgress/?",
        getGoodsType: "/integral/goods/getGoodsType",
        cancel: "/user/integral/goods/cancel/?",
        delete: "/user/integral/goods/delete/?",
        signFor: "/user/integral/goods/signFor/?",
    },
    page: {
        progress: "/pages/tabbar/user/integral/progress/index",
        GoodsDetail: "/pages/tabbar/user/integral/exchange/goods-detail/index"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
        this.initIntegralGoodsType();
        utils.saveFromPage(this,getCurrentPages());
    },
    onShow() {
    },

    // 获取积分商品
    initIntegralGoodsType() {
        App.$get(true, this.URL.getGoodsType, {}, res => {
            if (res.R) {
                this.setData({
                    'records.goodsType': res.data,
                    'records.activeKey': this.data.records.activeKey,
                })
                this.setData({
                    'searchParams': utils.isInvalid(this.data.searchParams) ? res.data[0].typeNo : this.data.searchParams
                })
                this.initIntegralGoods(this.data.page.pageNum, false);
            }
        })
    },
    initIntegralGoods(pageNum, loadMore) {
        let params = {
            pageNum: pageNum,
            pageSize: this.data.page.pageSize,
            typeNo: this.data.searchParams
        }
        this.setLoadingMore(loadMore, "loading", "加载中...")
        App.$get(!loadMore ? true : loadMore, this.URL.getList, params, res => {
            this.setData({
                "loadMore.show": false,
            })
            wx.stopPullDownRefresh();
            if (res.R) {
                this.setData({
                    "page.pageNum": res.data.pageNum,
                    "page.pageSize": res.data.pageSize,
                    "page.pages": res.data.pages
                })
                if (!loadMore) {
                    this.setData({
                        "records.list": res.data.rows,
                    })
                    return;
                }
                let sourceData = this.data.records.list;
                let newData = res.data.rows;
                newData.forEach(data => {
                    sourceData.push(data);
                })
                this.setData({
                    "records.list": sourceData,
                })
            }
        })
    },
    onGoodTypeChange(event) {
        let index = event.detail.index;
        let data = this.data.records.goodsType;
        this.setData({
            'searchParams': data[index].typeNo,
            'records.activeKey': index,
            'page.pageNum': 1,
        })
        this.initIntegralGoods(1, false);
    },
    goBack() {
        let pages = getCurrentPages();
        let prePage = utils.getFromPage(App, pages);
        App.$removeStore((pages[pages.length - 1]).route);
        App.$redirectTo(prePage, true)
    },
    showLagerImg(e) {
        let imgUrl = e.currentTarget.dataset.img;
        wx.previewImage({
            current: imgUrl,
            urls: [imgUrl]
        })
    },

    // 关闭物流信息
    closeExpress() {
        this.setData({
            'showMore.show': false,
        })
    },
    // 展开物流信息
    openExpressDetail(e) {
        let item = e.currentTarget.dataset.item;

        this.setData({
            'showMore.show': true,
            'records.address': item.address,
            'records.currentRecord': item,
        })
        // 查询物流进度
        App.$get(true, this.URL.progress.replace("?", item.userGoodsNo), {}, res => {
            if (res.R) {
                for (const item in res.data.progress) {
                    if (res.data.progress[item].isCurrentStatus) {
                        this.setData({
                            'records.progressActive': item
                        })
                        break;
                    }
                }
                this.setData({
                    "records.progress": res.data
                })
            }
        })
    },

    cancelRecord(e) {
        let item = e.currentTarget.dataset.item;
        if (!utils.isInvalid(item.expressNo)) {
            Toast("订单已发货，不可取消")
            return;
        }
        App.$post(true, this.URL.cancel.replace("?", item.userGoodsNo), {}, res => {
            this.reqResult(res);
        })
    },

    deleteRecord(e) {
        let item = e.currentTarget.dataset.item;
        if (item.expressStatus === '运输中') {
            Toast("订单已发货，不可删除")
            return;
        }
        App.$post(true, this.URL.delete.replace("?", item.userGoodsNo), {}, res => {
            this.reqResult(res);
        })
    },

    signFor(e) {
        let item = e.currentTarget.dataset.item;
        if (item.expressStatus === '取消') {
            Toast("订单已取消，不可签收")
            return;
        }

        if (item.expressStatus === '待发货') {
            Toast("订单尚未发货")
            return;
        }


        if (item.expressStatus === '已送达') {
            Toast("您已签收过了")
            return;
        }

        App.$post(true, this.URL.signFor.replace("?", item.userGoodsNo), {}, res => {
            this.reqResult(res);
        })
    },

    reqResult(res) {
        if (res.R) {
            this.initIntegralGoodsType();
        }
    },

    // 复制订单号
    copyOrderNo(e) {
        let orderNo = e.currentTarget.dataset.orderno;
        wx.setClipboardData({
            data: orderNo,
            success: function (res) {
                wx.getClipboardData({
                    success: function () {
                        wx.showToast({
                            title: '已复制',
                            icon:'none'
                        })
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.initIntegralGoods(1, false);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.page.pages === 0 || this.data.page.pageNum + 1 > this.data.page.pages) {
            this.setLoadingMore(true, "end", "我是有底线的~")
            return;
        }
        this.initIntegralGoods(this.data.page.pageNum + 1, true);
    },
    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
})
